// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import CustomComponents 1.0
import "values.js" as Values

Item
{

    y: 0

    id: eegChannel
    property string name: ""
    property string color: "green"

    smooth: true

    Component.onCompleted: init()

    property double mean: 0
    property double xScaling: 20.0

    function init()
    {
	Values.createArray(eegChannel.name);
	updateAllValues();
    }

    function updateAllValues()
    {


	for (var i = 0; i<page.values; ++i)
	{
	    Values.updateValue(eegChannel.name, i, 0)
	}
	for (var i = 0; i<(page.values-1); ++i)
	{
	    eegChannel.children[i].y1 = Values.getValue(eegChannel.name, i);
	    eegChannel.children[i].y2 = Values.getValue(eegChannel.name, i+1);
	}

    }

    function updateValues(current, vs)
    {
	var n = vs.length;




	var u = 0;
	for (var i = current - 1; i>=(current-n); --i)
	{
	    var cn = i;
	    if (cn < 0) cn = page.values + cn;
	    Values.updateValue(eegChannel.name, cn, vs[u]);
	    ++u;
	}

	eegChannel.mean = 0;
	for (var v = 0; v <page.values; ++v)
	{
	    eegChannel.mean += Values.getValue(eegChannel.name,v);
	}
	mean /= page.values;



	for (var k = 0; k<(page.values-1); ++k)
	{
	    eegChannel.children[k].y1 = (Values.getValue(eegChannel.name, k) - eegChannel.mean)/eegChannel.xScaling;
	    eegChannel.children[k].y2 = (Values.getValue(eegChannel.name, k+1) - eegChannel.mean)/eegChannel.xScaling;
	}

    }



    Repeater
    {
	model: page.values-1

	Line
	{
	    y1: 0
	    y2: 0
	    x1: index*page.valueToValueDistance
	    x2: (index+1)*page.valueToValueDistance
	    color: eegChannel.color
	    smooth: false
	    penWidth: 2

	}

    }

    /*
    Text
    {
	anchors.left: parent.left
	anchors.top: parent.top
	rotation: -90
	text: eegChannel.name
    }
    */

}
